﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


using System.IO;

using System.Runtime.Serialization.Formatters.Binary;



namespace FXnaInfrastructure
{
    public class FileHelper
    {


        public static void SaveToBinary(string filename, object obj)
        {
            if (filename == "" || obj == null)
            {
                throw new ArgumentNullException("文件或者obj为空");
            }
            else
            {
                BinaryFormatter bf = new BinaryFormatter();

                Stream stream = new FileStream(filename, FileMode.Create, FileAccess.Write);

                try
                {
                    bf.Serialize(stream, obj);
                }

               
                finally
                {
                    stream.Close();
                }

            }
        }

        public static object OpenFromBinary(string fname)
        {
            //这里的代码比较糟糕，没有考虑fname对应的文件不存在的情况等。

            BinaryFormatter bf = new BinaryFormatter();

            //将文件属性设为普通

            Stream stream = new FileStream(fname, FileMode.Open, FileAccess.Read, FileShare.None);

            object obj=null;

            try
            {
                obj = bf.Deserialize(stream);
            }

           
            finally
            {
                stream.Close();
            }

           
            return obj;


        }

        /// <summary>
        /// 将文本文件写入到硬盘
        /// </summary>
        /// <param name="pfname"></param>
        /// <returns></returns>
        public static void WriteFile(string pfname, string content)
        {


            FileInfo MyFile = new FileInfo(pfname);
            if (MyFile.Exists == true)
            {
                MyFile.Attributes = FileAttributes.Normal;
                MyFile.Delete();

            }




               

                  
                FileStream FS=null;
                StreamWriter Sw=null;

                try{

            
                        FS= MyFile.Create();


                        FS.Close();
                        MyFile.Attributes = FileAttributes.Normal;
                         Sw = MyFile.AppendText();

                        Sw.Write(content, Encoding.GetEncoding("GB2312"), false);
                        Sw.Flush();
                }



               


                finally
                {
                    FS.Close();

                    Sw.Close ();
                  
 
                }

           

               

            }




        /// <summary>
        /// 平行复制移动一个文件
        /// </summary>
        /// <param name="srcFName"></param>
        /// <param name="destFName"></param>
        /// <returns></returns>
        public static bool MoveFile(string srcFName, string destFName)
        {
            try
            {

              

              //必须手工保证目录的存在

                string destDirectory = Path.GetDirectoryName(destFName);

                if (!Directory.Exists(destDirectory))
                {
                    Directory.CreateDirectory(destDirectory);
                    
                }

                File.Copy(srcFName, destFName, true);

            }

            catch
            {

                return false;

            }

            return true;
 


        }


      

           

       

    }
}
